/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jbfilter.bean.fcomps.delegate;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.jbfilter.bean.PropertyAccessibleBean;
import org.jbfilter.bean.PropertyAccessor;
import org.jbfilter.core.fcomps.delegate.FilterComponentDelegate;
import org.jbfilter.core.fcomps.delegate.PropertyAccessibleDelegate;
/**
 * @author Marcus Adrian
 * @see FilterComponentDelegate
 * @see PropertyAccessibleBean
 */
public class PropertyAccessibleBeanDelegate<E, T>
	implements
		PropertyAccessibleBean<E, T> {
	
	private List<PropertyAccessibleBean<E, T>> pabs = new ArrayList<PropertyAccessibleBean<E,T>>();
	private PropertyAccessibleDelegate paDelegate = new PropertyAccessibleDelegate();

	public void add(PropertyAccessibleBean<E, T> pab) {
		pabs.add(pab);
		paDelegate.add(pab);
	}
	public void addAll(Collection<PropertyAccessibleBean<E, T>> pabs) {
		this.pabs.addAll(pabs);
	}
	@Override
	public String getPropertyPath() {
		return paDelegate.getPropertyPath();
	}
	@Override
	public void setPropertyPath(String propertyPath) {
		paDelegate.setPropertyPath(propertyPath);
	}
	@Override
	public T getPropertyValue(E bean) {
		for (PropertyAccessibleBean<E, T> pab : pabs) {
			return pab.getPropertyValue(bean);
		}
		throw new IllegalStateException("No single PropertyAccessibleBean set.");
	}
	@Override
	public void setPropertyAccessor(PropertyAccessor<E, T> propertyAccessor) {
		for (PropertyAccessibleBean<E, T> pab : pabs) {
			pab.setPropertyAccessor(propertyAccessor);
		}
	}

}
